java
User Input in Java (Scanner & BufferedReader)**
In any interactive program, you often need to accept data from users — such as names, numbers, or choices. In Java, there are several ways to read input, but the two most commonly used are:
* **`Scanner` class**
* **`BufferedReader` class**
Let’s explore both in detail. 
                  Using Scanner Class
The Scanner class is part of the java.util package and is the most beginner-friendly way to read user input. Key Features: Reads different data types directly (e.g., int, double, String). Easy to use for console-based programs.
import java.util.Scanner;
public class ScannerExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = sc.nextLine(); // Reads a line of text
        System.out.print("Enter your age: ");
        int age = sc.nextInt(); // Reads an integer
        System.out.println("Hello, " + name + "! You are " + age + " years old.");
        sc.close();
    }
}
How it works:
new Scanner(System.in) creates a Scanner object to read from the console.
nextLine() reads an entire line of text.
nextInt(), nextDouble(), etc., read specific data types.
Note:
After using numeric methods like nextInt() or nextDouble(), use nextLine() to consume the leftover newline character.
 
                  Using BufferedReader Class
BufferedReader is part of the java.io package and is more efficient for reading large amounts of text, but it only reads Strings. You need to convert strings to numbers manually if needed.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderExample {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter your city: ");
        String city = br.readLine(); // Reads a line of text
        System.out.print("Enter your pincode: ");
        int pincode = Integer.parseInt(br.readLine()); // Convert String to int
        System.out.println("You live in " + city + " with pincode " + pincode);
    }
} 
                  Comparison Table
Feature Scanner BufferedReader Package java.util java.io Reads different types Yes (int, double, String, etc.) No (only String) Conversion needed No Yes Speed Slower for large input Faster for large input Ease of use Easier for beginners Requires more manual work
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderExample {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter your city: ");
        String city = br.readLine(); // Reads a line of text
        System.out.print("Enter your pincode: ");
        int pincode = Integer.parseInt(br.readLine()); // Convert String to int
        System.out.println("You live in " + city + " with pincode " + pincode);
    }
} 
                  Best Practices
Use Scanner for small programs and mixed-type inputs. Use BufferedReader when reading large text files or performance matters. Always close the input stream when done (scanner.close() or br.close()). Handle exceptions when working with BufferedReader (IOException).